package com.ruoyi.aftserv.controller;

import com.ruoyi.aftserv.domain.AfterServiceApply;
import com.ruoyi.aftserv.service.IAfterServiceApplyService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 报名方案管理Controller
 * 
 * @author 张艳羽
 * @date 2024-08-21
 */
@Controller
@RequestMapping("/aftserv/apply")
public class AfterServiceApplyController extends BaseController
{
    private String prefix = "aftserv/apply";

    @Autowired
    private IAfterServiceApplyService afterServiceApplyService;

    @RequiresPermissions("aftserv:apply:view")
    @GetMapping()
    public String apply()
    {
        return prefix + "/apply";
    }

    /**
     * 查询报名方案管理列表
     */
    @RequiresPermissions("aftserv:apply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AfterServiceApply afterServiceApply)
    {
        startPage();
        List<AfterServiceApply> list = afterServiceApplyService.selectAfterServiceApplyList(afterServiceApply);
        return getDataTable(list);
    }

    /**
     * 导出报名方案管理列表
     */
    @RequiresPermissions("aftserv:apply:export")
    @Log(title = "报名方案管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AfterServiceApply afterServiceApply)
    {
        List<AfterServiceApply> list = afterServiceApplyService.selectAfterServiceApplyList(afterServiceApply);
        ExcelUtil<AfterServiceApply> util = new ExcelUtil<AfterServiceApply>(AfterServiceApply.class);
        return util.exportExcel(list, "报名方案管理数据");
    }

    /**
     * 新增报名方案管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存报名方案管理
     */
    @RequiresPermissions("aftserv:apply:add")
    @Log(title = "报名方案管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AfterServiceApply afterServiceApply)
    {
        return toAjax(afterServiceApplyService.insertAfterServiceApply(afterServiceApply));
    }

    /**
     * 修改报名方案管理
     */
    @RequiresPermissions("aftserv:apply:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AfterServiceApply afterServiceApply = afterServiceApplyService.selectAfterServiceApplyById(id);
        mmap.put("afterServiceApply", afterServiceApply);
        return prefix + "/edit";
    }

    /**
     * 修改保存报名方案管理
     */
    @RequiresPermissions("aftserv:apply:edit")
    @Log(title = "报名方案管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AfterServiceApply afterServiceApply)
    {
        return toAjax(afterServiceApplyService.updateAfterServiceApply(afterServiceApply));
    }

    /**
     * 删除报名方案管理
     */
    @RequiresPermissions("aftserv:apply:remove")
    @Log(title = "报名方案管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(afterServiceApplyService.deleteAfterServiceApplyByIds(ids));
    }

    @RequiresPermissions("aftserv:apply:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(AfterServiceApply apply)
    {
        return toAjax(afterServiceApplyService.updateAfterServiceApply(apply));
    }

    /**
     * 加载课后服务报名方案
     */
    @RequiresPermissions("aftserv:apply:list")
    @GetMapping("/applyListData")
    @ResponseBody
    public List<Ztree> applyListData()
    {
        List<AfterServiceApply> applyList = this.afterServiceApplyService.selectAfterServiceApplyList(new AfterServiceApply());
        List<Ztree> ztrees = initZtree(applyList);
        return ztrees;
    }
    public List<Ztree> initZtree(List<AfterServiceApply> applyList)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (AfterServiceApply apply : applyList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(apply.getId());
            ztree.setName(sdf.format(apply.getApplyMonth()));
            ztree.setTitle(sdf.format(apply.getApplyMonth()));
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
